package gdut.jmxywz.action;


import java.io.File;
import java.io.FileInputStream;
import java.sql.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import gdut.jmxywz.been.Dissertation;
import gdut.jmxywz.been.Prize;
import gdut.jmxywz.been.Teacher;
import gdut.jmxywz.util.PageModel;

import com.opensymphony.xwork2.ActionContext;


public class PrizeAction extends BaseAction{
	private  static final String  TEACHER_SESSION_KEY = "teacherSessionKey";
	private Teacher  teacher;
	
	private Prize prize;
	 private PageModel pageModel;
	 private int prizeId;
	 
		/***************分页*****************/
		private int currentPage=1;
		
		private int pageSize=5;
		
		/***************检索*****************/
		//private String searchSelect="";
		
		private String like="";
		private String where_search="";
		private String where_value="";
		//private String searchValue="";
		
		/**************excel表导出导入****************/
		private File excelFile;
		private String excelFileContentType;
		private boolean checkbox;


	
	
		/***************增加*****************/
	 public String add(){
		 
		 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		 prize.setTeacherByEditer(teacher);
		 boolean bool=this.getCommonService().saveObject(prize);
		 
		 if(bool)
		 return SUCCESS;
		 else
			 return "error";
	 }

	 
	 
	 /***************show*****************/
		public String show()throws Exception{
			
			String hql=" from Prize where auditing != 0";
			hql = hql +" order by prizeId desc,auditing desc";
			pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"PrizeShow.action","");
//			scientificInfo=(Info)pageModel.getList().get(0);
//			System.out.println("============================"+scientificInfo.getInfoTitle());
			
			return SUCCESS;
		}
		
		
		/***************查找*****************/
		public String search() throws Exception {
			// TODO Auto-generated method stub
			String hql="from Prize where auditing != 0";
			
			 String[] parem = null;
	    	  
	    	  String  search_parameter = "";
	    	  String en_where_value="";
	    	  
	    	  
				HttpServletRequest request = ServletActionContext.getRequest();
				where_value=new String(request.getParameter("where_value").getBytes("ISO-8859-1"),"UTF-8");
				//
		    	  String searchValue1=where_value;
		    	  //
				//
         if(!"".equals(where_value)&&where_value!=null){
	    		  
	    		  en_where_value=java.net.URLEncoder.encode(where_value, "utf-8");//URL编码转换
	    		  System.out.println("++++-- "+where_value);
	    		  
	    		  if (like.equals("like")) {
	    			  hql = hql + "  and " + where_search + " like '%"+ searchValue1 + "%' ";
	  				//hql = hql + "  and sc." + where_search + " like '%"+ searchValue1 + "%' ";
	  			} else {
	  				hql = hql + "  and " + where_search + " ='"+ searchValue1 + "' ";
	  				//hql = hql + "  and sc." + where_search + " ='"+ searchValue1 + "' ";
	  			}
	    		  
	    	  }
	    	  
         hql = hql +" order by prizeId desc,auditing desc";
	    	  
	    	  //查询条件
	    	  
	    	  search_parameter="&where_search="+ where_search+"&like="+like+"&where_value="+en_where_value;
	    	  
	    	  pageModel = new PageModel();
	    	  
	    	  pageModel=this.getCommonService().findPage(hql, parem, currentPage, pageSize, "PrizeSearch.action",search_parameter);
			
			
		
			return SUCCESS;
			
			
		}

		
		
		
		/***************详细*****************/
		public String detail() throws Exception {
			// TODO Auto-generated method stub
			prize=(Prize)this.getCommonService().findByIntId(prizeId, "gdut.jmxywz.been.Prize");
			if(prize==null)
				return "error";
			else
				return SUCCESS;
		}

		
		
		
		
		/***************更新预处理*****************/
		public String preUpdate() throws Exception {
			// TODO Auto-generated method stub
			 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

			
			prize=(Prize)this.getCommonService().findByIntId(prizeId, "gdut.jmxywz.been.Prize");
			prize.setTeacherByAssessor(teacher);
			if(prize==null)
				return "error";
			else
				return SUCCESS;
		}
		
		/***************非管理员更新预处理*****************/
		public String preUpdateSelf() throws Exception {
			 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

			// TODO Auto-generated method stub
			prize=(Prize)this.getCommonService().findByIntId(prizeId, "gdut.jmxywz.been.Prize");
			if(!prize.getTeacherByEditer().getTeacherId().equals(teacher.getTeacherId())){
				
				System.out.println("没有修改的权限");
				
				return "error";
			}
			if(!"0".equals(prize.getAuditing())){
		   		  
		   		System.out.println("已通过审核，不能在修改！");
		   		
		   		return "error";
		   	  }
		 
			if(prize==null)
				return "error";
			else
				return SUCCESS;
		}
		
		
		/***************更新*****************/
		public String update() throws Exception {
			
			Prize prize2=(Prize)this.getCommonService().findByIntId(prize.getPrizeId(), "gdut.jmxywz.been.Prize");
			prize2.setAwardType(prize.getAwardType());
			prize2.setAwardUnit(prize.getAwardUnit());
			prize2.setItemName(prize.getItemName());
			prize2.setPublicationYear(prize.getPublicationYear());
			prize2.setStep(prize.getStep());
			prize2.setWinner(prize.getWinner());
			prize2.setSysId(prize.getSysId());
			prize2.setPrizeName(prize.getPrizeName());
			prize2.setTypeFirstName(prize.getTypeFirstName());
			prize2.setFirstName(prize.getFirstName());
			prize2.setSuosudanwei(prize.getSuosudanwei());
			prize2.setTeachingSedtion(prize.getTeachingSedtion());
			prize2.setPrizeNumber(prize.getPrizeNumber());
			prize2.setSchoolSign(prize.getSchoolSign());
			prize2.setWinningLevel(prize.getWinningLevel());
			prize2.setCooperateDartner(prize.getCooperateDartner());
			prize2.setRanking(prize.getRanking());
			prize2.setCertificateID(prize.getCertificateID());
			prize2.setXuekemenlei(prize.getXuekemenlei());
			prize2.setCategory(prize.getCategory());
			prize2.setItemSource(prize.getItemSource());
			prize2.setPrizeForm(prize.getPrizeForm());
			prize2.setDeliberateWay(prize.getDeliberateWay());
			prize2.setAchieveLevel(prize.getAchieveLevel());
			prize2.setPrizeLevel(prize.getPrizeLevel());
			prize2.setInfro(prize.getInfro());
			prize2.setRemark(prize.getRemark());
			prize2.setAwardClass(prize.getAwardClass());
			prize2.setCertificate(prize.getCertificate());
			// TODO Auto-generated method stub
			boolean bl=this.getCommonService().updateObject(prize2);
			if(bl)
				return SUCCESS;
			else
				return "error";
		}


        //导入文件
	    @SuppressWarnings("unchecked")
		public String importExcel() throws Exception {
			 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

	
	    	// TODO Auto-generated method stub
			if(excelFile==null)
			{
				return "error";
				}
					

	    	int count=0;
			try{
			FileInputStream fis=new FileInputStream(excelFile);
			jxl.Workbook jwb=Workbook.getWorkbook(fis);
			int sheets=jwb.getNumberOfSheets();
			for(int i=0;i<sheets;i++){
				Sheet rs=jwb.getSheet(i);
				int columnNum=rs.getColumns();
				int rowNum=rs.getRows();
				System.out.println("======="+rowNum+"----"+columnNum);
				if(rowNum<0&&columnNum!=29){
					System.out.println("--------------------导入格式不对----------------------");
					return "error";
				}
				Cell crl;
				for(int row=1;row<rowNum;row++){
					
//					String hql="from Dissertation stc where stc.isbn='"+crl.getContents()+"'";
//					List<Dissertation> aa=this.getCommonService().findListByHql(hql, null);
					

	                 boolean bl;
	             
//					if(aa.size()==0){//项目未存在
						
						prize=new Prize();
						crl=rs.getCell(0,row);
						prize.setSysId(crl.getContents().trim());
						 crl=rs.getCell(1,row);
                        prize.setPrizeName(crl.getContents().trim());
                        crl=rs.getCell(2,row);
                        prize.setTypeFirstName(crl.getContents().trim());
                        crl=rs.getCell(3,row);
                        prize.setFirstName(crl.getContents().trim());
                        crl=rs.getCell(4,row);
                        prize.setItemName(crl.getContents().trim());
                        crl=rs.getCell(5,row);
                        prize.setSuosudanwei(crl.getContents().trim());
                        crl=rs.getCell(6,row);
                        prize.setTeachingSedtion(crl.getContents().trim());
                        crl=rs.getCell(7,row);
                        prize.setWinner(crl.getContents().trim());
                        crl=rs.getCell(8,row);
                        prize.setPrizeNumber(crl.getContents().trim());
                        crl=rs.getCell(9,row);
                        prize.setAwardUnit(crl.getContents().trim());                      
                       // crl=rs.getCell(10,row);                      
                        //Date now=Date.valueOf(crl.getContents().trim());
                       // prize.setPublicationYear(now);
                        crl=rs.getCell(11,row);
                        prize.setSchoolSign(crl.getContents().trim());
                        crl=rs.getCell(12,row);
                        prize.setWinningLevel(crl.getContents().trim());
                        crl=rs.getCell(13,row);
                        prize.setStep(crl.getContents().trim());
                        crl=rs.getCell(14,row);
                        prize.setCooperateDartner(crl.getContents().trim());
                        crl=rs.getCell(15,row);
                        prize.setRanking(crl.getContents().trim());
                        crl=rs.getCell(16,row);
                        prize.setCertificateID(crl.getContents().trim());
                        crl=rs.getCell(17,row);
                        prize.setXuekemenlei(crl.getContents().trim());
                        crl=rs.getCell(18,row);
                        prize.setCategory(crl.getContents().trim());
                        crl=rs.getCell(19,row);
                        prize.setItemSource(crl.getContents().trim());
                        crl=rs.getCell(20,row);
                        prize.setAwardType(crl.getContents().trim());
                        crl=rs.getCell(21,row);
                        prize.setPrizeForm(crl.getContents().trim());
                        crl=rs.getCell(22,row);
                        prize.setDeliberateWay(crl.getContents().trim());
                        crl=rs.getCell(23,row);
                        prize.setAchieveLevel(crl.getContents().trim());
                        crl=rs.getCell(24,row);
                        prize.setPrizeLevel(crl.getContents().trim());
                        crl=rs.getCell(25,row);
                        prize.setInfro(crl.getContents().trim());
                        crl=rs.getCell(26,row);
                        prize.setRemark(crl.getContents().trim());
                        crl=rs.getCell(27,row);
                        prize.setAwardClass(crl.getContents().trim());
                        crl=rs.getCell(28,row);
                        prize.setCertificate((crl.getContents().trim()));
                        prize.setTeacherByEditer(teacher);
                        prize.setAuditing("0");
						bl=this.getCommonService().saveObject(prize);//不存在该项目

						if(!bl){
							System.out.println("------------------存入数据库出错-------------------");
						}
						
						count++;
						
					}
					
			}
			System.out.println("=================一共导入数据数：================="+count);
			jwb.close();
			fis.close();
			}catch(Exception e){
				e.printStackTrace();
			}
			return SUCCESS;
		}
		
		
		/***************删除*****************/
		public String delete() throws Exception {
			  teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

			// TODO Auto-generated method stub
			prize=(Prize)this.getCommonService().findByIntId(prizeId, "gdut.jmxywz.been.Prize");
			if(prize!=null){
				boolean bl=this.getCommonService().deleteObject(prize);
				if(bl)
					return SUCCESS;
				else
					return "error";
				}else{
					return "error";
				}
		}
		/***************非管理员删除*****************/
		public String deleteSelf() throws Exception {
			 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

			// TODO Auto-generated method stub
			prize=(Prize)this.getCommonService().findByIntId(prizeId, "gdut.jmxywz.been.Prize");
			//判断是否可以删除
	  	    if(!prize.getTeacherByEditer().getTeacherId().equals(teacher.getTeacherId())){
				
				System.out.println("没有删除的权限");
				
				return "error";
			}

			if(!"0".equals(prize.getAuditing())){
		   		  
		   		System.out.println("已通过审核，不能在修改！");
		   		
		   		return "error";
		   	  }
			if(prize!=null){
				boolean bl=this.getCommonService().deleteObject(prize);
				if(bl)
					return SUCCESS;
				else
					return "error";
				}else{
					return "error";
				}
		}
		
		
		
		/***************show审核信息列表*****************/
		public String auditingShow()throws Exception{
			 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

			String hql=" from Prize ";
			 hql = hql +"  order by auditing asc ,prizeId desc";
			pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"PrizeAuditingShow.action","");
			
			return SUCCESS;
		}
		/***************show审核信息列表*****************/
		public String selfShow()throws Exception{
			 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

			String hql=" from Prize where teacherByEditer.teacherId="+teacher.getTeacherId();
			 hql = hql +"  order by auditing asc ,prizeId desc";
			pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"PrizeSelfShow.action","");
			
			return SUCCESS;
		}
		
		
		
		
		/***************Search管理员审核列表*****************/		
		public String searchAuditing() throws Exception {
			 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

			// TODO Auto-generated method stub
			String hql="from Prize";
			
			 String[] parem = null;
	    	  
	    	  String  search_parameter = "";
	    	  String en_where_value="";
	    	  
	    	  
				HttpServletRequest request = ServletActionContext.getRequest();
				where_value=new String(request.getParameter("where_value").getBytes("ISO-8859-1"),"UTF-8");
				//
		    	  String searchValue1=where_value;
		    	  //
				//
        if(!"".equals(where_value)&&where_value!=null){
	    		  
	    		  en_where_value=java.net.URLEncoder.encode(where_value, "utf-8");//URL编码转换
	    		  System.out.println("++++-- "+where_value);
	    		  
	    		  if (like.equals("like")) {
	    			  hql = hql + "  where " + where_search + " like '%"+ searchValue1 + "%' ";
	  				//hql = hql + "  and sc." + where_search + " like '%"+ searchValue1 + "%' ";
	  			} else {
	  				hql = hql + "  where " + where_search + " ='"+ searchValue1 + "' ";
	  				//hql = hql + "  and sc." + where_search + " ='"+ searchValue1 + "' ";
	  			}
	    		  
	    	  }
	    	  
        hql = hql +"  order by auditing asc ,prizeId desc";
	    	  
	    	  //查询条件
	    	  
	    	  search_parameter="&where_search="+ where_search+"&like="+like+"&where_value="+en_where_value;
	    	  
	    	  pageModel = new PageModel();
	    	  
	    	  pageModel=this.getCommonService().findPage(hql, parem, currentPage, pageSize, "PrizeSearchAuditing.action",search_parameter);
			
			
			return SUCCESS;
		}
		
		/***************Search非管理员审核列表*****************/		
		public String searchSelf() throws Exception {
			 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

			// TODO Auto-generated method stub
			String hql="from Prize";
			//
			
			
			
			 String[] parem = null;
	    	  
	    	  String  search_parameter = "";
	    	  String en_where_value="";
	    	  
	    	  
				HttpServletRequest request = ServletActionContext.getRequest();
				where_value=new String(request.getParameter("where_value").getBytes("ISO-8859-1"),"UTF-8");
				//
		    	  String searchValue1=where_value;
		    	  //
				//
       if(!"".equals(where_value)&&where_value!=null){
	    		  
	    		  en_where_value=java.net.URLEncoder.encode(where_value, "utf-8");//URL编码转换
	    		  System.out.println("++++-- "+where_value);
	    		  
	    		  if (like.equals("like")) {
	    			  hql = hql + "  where " + where_search + " like '%"+ searchValue1 + "%' ";
	  				//hql = hql + "  and sc." + where_search + " like '%"+ searchValue1 + "%' ";
	  			} else {
	  				hql = hql + "  where " + where_search + " ='"+ searchValue1 + "' ";
	  				//hql = hql + "  and sc." + where_search + " ='"+ searchValue1 + "' ";
	  			}
	    		  
	    	  }
	    	  
       hql = hql +"  order by auditing asc ,prizeId desc";
	    	  
	    	  //查询条件
	    	  
	    	  search_parameter="&where_search="+ where_search+"&like="+like+"&where_value="+en_where_value;
	    	  
	    	  pageModel = new PageModel();
	    	  
	    	  pageModel=this.getCommonService().findPage(hql, parem, currentPage, pageSize, "PrizeSearchSelf.action",search_parameter);
			
		
			
			return SUCCESS;
		}
		
		
		   /***
	     *   inside 对内公布
	     */
		  public String inside() throws Exception{
				 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

	    	  
			  prize=(Prize)this.getCommonService().findByIntId(prizeId, "gdut.jmxywz.been.Prize");
	    	  //将信息审核为对院内公布 ，即设置auditing为1；
			 if(prize!=null){
				 prize.setAuditing("1");
				 prize.setTeacherByAssessor(teacher);
			  boolean bl=this.getCommonService().updateObject(prize);
			   if(bl){  
			  return SUCCESS;
			   }
			   return "error";
			 }
			 return "error";
		  }
		  
		
	    /***
	     *   outside 对外公布
	     */
	    public String outside() throws Exception{
			 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

	    	prize=(Prize)this.getCommonService().findByIntId(prizeId, "gdut.jmxywz.been.Prize");
	    	  //将信息审核为对院内公布 ，即设置auditing为1；
			 if(prize!=null){
				 prize.setAuditing("2");
				 prize.setTeacherByAssessor(teacher);
			  boolean bl=this.getCommonService().updateObject(prize);
			   if(bl){  
			  return SUCCESS;
			   }
			   return "error";
			 }
			 return "error";
		  }
		
		/***
		 * 
		 *    notAllow 不通过
		 */
	    public String notAllow() throws Exception{
			 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

	    	prize=(Prize)this.getCommonService().findByIntId(prizeId, "gdut.jmxywz.been.Prize");
	    	  //将信息审核为对院内公布 ，即设置auditing为1；
			 if(prize!=null){
				 prize.setAuditing("3");
				 prize.setTeacherByAssessor(teacher);
			  boolean bl=this.getCommonService().updateObject(prize);
			   if(bl){  
			  return SUCCESS;
			   }
			   return "error";
			 }
			 return "error";
		  }
		
	    
	    
		
		/***
		 * 
		 *    rollBack 未审核
		 */
	    public String rollBack() throws Exception{
			 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

	    	prize=(Prize)this.getCommonService().findByIntId(prizeId, "gdut.jmxywz.been.Prize");
	    	  //将信息审核为对院内公布 ，即设置auditing为1；
			 if(prize!=null){
				 prize.setAuditing("0");
				 prize.setTeacherByAssessor(teacher);
			  boolean bl=this.getCommonService().updateObject(prize);
			   if(bl){  
			  return SUCCESS;
			   }
			   return "error";
			 }
			 return "error";
	  	  }



	    
	    
	    
		public Teacher getTeacher() {
			return teacher;
		}



		public void setTeacher(Teacher teacher) {
			this.teacher = teacher;
		}



		public Prize getPrize() {
			return prize;
		}



		public void setPrize(Prize prize) {
			this.prize = prize;
		}



		public PageModel getPageModel() {
			return pageModel;
		}



		public void setPageModel(PageModel pageModel) {
			this.pageModel = pageModel;
		}



		public int getPrizeId() {
			return prizeId;
		}



		public void setPrizeId(int prizeId) {
			this.prizeId = prizeId;
		}



		public int getCurrentPage() {
			return currentPage;
		}



		public void setCurrentPage(int currentPage) {
			this.currentPage = currentPage;
		}



		public int getPageSize() {
			return pageSize;
		}



		public void setPageSize(int pageSize) {
			this.pageSize = pageSize;
		}



	/*	public String getSearchSelect() {
			return searchSelect;
		}



		public void setSearchSelect(String searchSelect) {
			this.searchSelect = searchSelect;
		}*/



		public String getLike() {
			return like;
		}



		public void setLike(String like) {
			this.like = like;
		}


/*
		public String getSearchValue() {
			return searchValue;
		}



		public void setSearchValue(String searchValue) {
			this.searchValue = searchValue;
		}
		public File getExcelFile() {
			return excelFile;
		}*/



		public void setExcelFile(File excelFile) {
			this.excelFile = excelFile;
		}



		public String getExcelFileContentType() {
			return excelFileContentType;
		}



		public void setExcelFileContentType(String excelFileContentType) {
			this.excelFileContentType = excelFileContentType;
		}



		public boolean isCheckbox() {
			return checkbox;
		}



		public void setCheckbox(boolean checkbox) {
			this.checkbox = checkbox;
		}



		public String getWhere_search() {
			return where_search;
		}



		public void setWhere_search(String whereSearch) {
			where_search = whereSearch;
		}



		public String getWhere_value() {
			return where_value;
		}



		public void setWhere_value(String whereValue) {
			where_value = whereValue;
		}

		
	
}
